package org.eden.web.controller;

import org.eden.rpc.common.entity.dao.User;
import org.eden.rpc.common.exception.ConnectException;
import org.eden.rpc.common.service.UserService;
import org.eden.rpc.core.client.pooled.DefaultRpcClientPool;
import org.eden.web.pojo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {

    @Autowired
    DefaultRpcClientPool clientPool;

    @PostMapping("/user")
    public Response register(User user) {
        try {
            UserService service = clientPool.getService(UserService.class);
            Long id = service.register(user);
            clientPool.retrieve();
            return Response.success(id);
        } catch (ConnectException e) {
            return Response.failed(e.getMessage());
        }
    }

    @GetMapping("/user/{id}")
    public Response getUserDetail(@PathVariable Long id) {
        try {
            UserService service = clientPool.getService(UserService.class);
            User user = service.getById(id);
            clientPool.retrieve();
            return Response.success(user);
        } catch (ConnectException e) {
            return Response.failed(e.getMessage());
        }
    }

}
